package com.digital.herbarium.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.digital.herbarium.Entity.Dna;
import com.digital.herbarium.Entity.Stat;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.DnaMapper;
import com.digital.herbarium.Util.ExcelUtilHandler;
import com.digital.herbarium.Util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DnaService {

    @Autowired
    private DnaMapper dnaMapper;


    public PageInfo<Dna> getDnaInfo (Dna dna, int pageSize, int pageNum){
        PageHelper.startPage(pageNum, pageSize);
        List<Dna> ilist =  dnaMapper.getDnaInfo(dna);
        PageInfo<Dna> pageInfo = new PageInfo<>(ilist);
        return  pageInfo;
    }

    public HashMap<String, Object> createList(Dna dna, int pSize, int pNum){
        HashMap<String, Object> map = new HashMap<>();
        int currPage = (pNum - 1) * pSize;
        dna.setpNum(currPage);
        dna.setpSize(pSize);
        List<Dna> list = dnaMapper.getTabList(dna);
        List<Dna> dnaInfo = dnaMapper.getDnaInfo(dna);
        List<Dna> tlist = dnaMapper.getAllType();
        list.stream().forEach(d1 -> {
            List<Dna> dnaList = getDna(d1, dnaInfo);
            // 添加二级分类
            List<Dna> dl = new ArrayList<>();
            tlist.stream().forEach(tl ->{
                Dna d = new Dna();
                d.setSequenceType(tl.getSequenceType());
                d.setTabList(getDnaType(tl, dnaList));
                dl.add(d);
            });
            d1.setTabList(dl);
        });
        map.put("list",list);
        map.put("total",dnaMapper.getTabTotal());
        return map;
    }

    public List<Dna> getDnaType(Dna dna, List<Dna> dlist){
        List<Dna> dl = new ArrayList<>();
        dlist.stream().forEach(at ->{
            if(at.getSequenceType() != null && dna.getSequenceType() != null){
                if(at.getSequenceType().equals(dna.getSequenceType())){
                    dl.add(at);
                }
            }


        });
        return  dl;
    }

    public List<Dna> getDna(Dna dna, List<Dna> dlist){
        List<Dna> d = new ArrayList<>();
        dlist.stream().forEach(d1 ->{
            if(d1.getMedicineName() != null && dna.getMedicineName() != null &&
                    d1.getOriginalPlant() != null && dna.getOriginalPlant() != null
                    && d1.getLatinName() != null &&  dna.getLatinName() != null){
                if(d1.getMedicineName().equals(dna.getMedicineName()) &&
                        d1.getOriginalPlant().equals(dna.getOriginalPlant())
                        && d1.getLatinName().equals(dna.getLatinName())){
                    d.add(d1);
                }
            }


        });
        return  d;
    }

    public void addDnaInfo(Dna dna){
        check(dna.getSampleNumber());
        dna.setState(1);
        dna.setCreatetime(TimeUtil.getNow());
        dnaMapper.addDnaInfo(dna);

    }

    public void check(String sampleNumber){
        Dna d = new Dna();
        d.setSampleNumber(sampleNumber);
        List<Dna> dlist = dnaMapper.getDnaInfoByNumber(d);
        if(dlist != null && dlist.size() > 0){
            throw new CustomException(400,"该采样编号已存在");
        }
    }

    public void editDnaInfo(Dna dna){
        check(dna.getSampleNumber());
        dnaMapper.editDnaInfo(dna);
    }

    public  void delDnaInfo(String id) {
        if (StrUtil.hasEmpty(id) || StrUtil.hasBlank(id)) {
            throw new CustomException(400, "缺少信息id");
        }
        String[] strArr = id.split(",");
        Long[] ids = Convert.toLongArray(strArr);
        dnaMapper.delDnaInfo(ids);
    }

    public void  addExcel(MultipartFile file, HttpServletRequest request, String uploadPath){
        ExcelReader reader = ExcelUtilHandler.getExcelReader(file, request, uploadPath);
        reader.addHeaderAlias("序列编号","sequenceNumber");
        reader.addHeaderAlias("采样编号","sampleNumber");
        reader.addHeaderAlias("药材名","medicineName");
        reader.addHeaderAlias("基原植物","originalPlant");
        reader.addHeaderAlias("拉丁名","latinName");
        reader.addHeaderAlias("样品来源","sampleSource");
        reader.addHeaderAlias("取样部位","smapleLocation");
        reader.addHeaderAlias("样品提供单位","sampleUnit");
        reader.addHeaderAlias("鉴定人","appraiser");
        reader.addHeaderAlias("责任人","dutyPerson");
        reader.addHeaderAlias("序列提交时间","sequenceTime");
        reader.addHeaderAlias("序列类型","sequenceType");
        reader.addHeaderAlias("采集信息","collectInfo");
        reader.addHeaderAlias("描述信息","describeInfo");
        List<Dna> dnaList = reader.readAll( Dna.class);
        dnaList.stream().forEach(dna -> {
            dna.setCreatetime(TimeUtil.getNow());
        });
        dnaMapper.bachAdd(dnaList);


    }

    public  List<Stat> getStatGroupByType(){
        return dnaMapper.getStatGroupByType();
    }

    public Integer getTotal(){
        return  dnaMapper.getTotal(null);
    }

    public Integer getYesterdayTotal(){
        String yesterday = TimeUtil.getYesterday();
        return dnaMapper.getTotal(yesterday);
    }

    public Integer getNowTotal(){
        String now = TimeUtil.getNow();
        return dnaMapper.getTotal(now);
    }

    public List<Stat> getTotalGroupByDate(String startTime,String endTime){
        return dnaMapper.getTotalGroupByDate(startTime,endTime);
    }
}
